home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4753 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: Tue, 6 Feb 1996 21:41:37 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9602062041.AA18117@dxmint.cern.ch>
  8. References: <11f7cc$17261a.3b3@daprez> <9602011151.AA04526@dxmint.cern.ch> <31177B86.41C67EA6@texas.net>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: relay-4.mail.demon.net!post.demon.co.uk!dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. Michael Douglass <mikedoug@texas.net> writes:
  14.  
  15. >Dan Pop wrote:
  16. >
  17. >> So:
  18. >> 
  19. >> 1. Never use ! with strcmp.  It's a good way to shoot yourself in the foot.
  20. >
  21. >Hmmm... I've *never* had any problems with do that...  Could you please
  22. >clarify why it would cause you to shoot yourself in the foot?
  23.  
  24. The bunch of wrong posts in this very thread had already clarified my
  25. point :-)
  26.  
  27. >> 2. Always check what a function is supposed to return before using it.
  28. >>    strcmp returns 0 when the strings are equal.
  29. >
  30. >I think he knew that, he just had hist || and && logic backwards.
  31.  
  32. Count yourself amongst the people who were fooled by his style :-)
  33. The original code looked like this:
  34.  
  35.     if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) Usage();
  36.  
  37. Now, if we apply your "correction" the result is:
  38.  
  39.     if (!strcmp(argv[1],"-d") && !strcmp(argv[1],"-e")) Usage();
  40.  
  41. which means that Usage() will be _never_ called, because this line is
  42. equivalent to 
  43.  
  44.     if (strcmp(argv[1],"-d") == 0 && strcmp(argv[1],"-e") == 0) Usage();
  45.  
  46. but argv[1], no matter its contents, cannot simultaneously compare equal
  47. to "-d" _and_ "-e"!
  48.  
  49. Dan
  50. -- 
  51. Dan Pop
  52. CERN, CN Division
  53. Email: danpop@mail.cern.ch 
  54. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  55.